//
//  ZWLRecommandViewController.swift
//  ZWLDY
//
//  Created by zwl on 2016/10/2.
//  Copyright © 2016年 zwl. All rights reserved.
//

import UIKit

private let KMargin : CGFloat = 10
private let KItemW : CGFloat = (kScreenW - KMargin * 3) / 2
private let KItemH : CGFloat = KItemW * 3 / 4
private let kHighItemH = KItemW * 4 / 3
private let KGameViewH : CGFloat = 90

private let KRemCellID = "KRemCellID"
private let KRemHighhCellID = "KRemHighhCellID"
private let kHeaderViewID = "kHeaderViewID"
private let kHeaderViewH : CGFloat = 50

private let KCycleViewH = kScreenW * 3 / 8

private let KliveBtnW : CGFloat = 40
private let KliveBtnH : CGFloat = 40

class ZWLRecommandViewController: ZWLAnimViewController {
    
    fileprivate lazy var RemVM : ZWLHomeVM = ZWLHomeVM()
    
    
    // MARK:- 懒加载控件
    
    fileprivate lazy var liveBtn : UIButton = {
        let btn = UIButton()
        btn.backgroundColor = UIColor.purple
        btn.frame = CGRect(x: kScreenW - KliveBtnW - KMargin, y: kScreenH - kTabbarH - KliveBtnH - KMargin - 108, width: KliveBtnW, height: KliveBtnH)
        return btn
    }()
    
    lazy var gameView : ZWLGameView = {
        let gameView = ZWLGameView.gameView()
        gameView.frame = CGRect(x: 0, y: -KGameViewH, width: kScreenW, height: KGameViewH)
        return gameView
    }()
    
    fileprivate lazy var cycleView : ZWLClcleView = {
        let cycleView = ZWLClcleView.clcleView()
        cycleView.frame = CGRect(x: 0, y: -KCycleViewH - KGameViewH, width: kScreenW, height: KCycleViewH)
        return cycleView
    }()
    
    fileprivate lazy var collectionView : UICollectionView = { [weak self] in
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: KItemW, height: KItemH)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = KMargin
        layout.sectionInset = UIEdgeInsets(top: 0, left: KMargin, bottom: 0, right: KMargin)
        layout.headerReferenceSize = CGSize(width: kScreenW, height: kHeaderViewH)
        let F = CGRect(x: 0, y: 0, width: kScreenW, height: kScreenH - kStatusBarH - kNavigationBarH - KpagetitleH - kTabbarH)
        let collectionView = UICollectionView(frame: F, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.white
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.contentInset = UIEdgeInsets(top: KCycleViewH + KGameViewH, left: 0, bottom: 0, right: 0)
        collectionView.register( UINib(nibName: "ZWLContentCell", bundle: nil), forCellWithReuseIdentifier: KRemCellID)
        collectionView.register( UINib(nibName: "ZWLContentHighCell", bundle: nil), forCellWithReuseIdentifier: KRemHighhCellID)
        // 注册头部视图
        collectionView.register(UINib(nibName: "ZWLHeaderCollectionViewCell", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: kHeaderViewID)
        
        return collectionView
    }()
    
    

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        
        // 设置UI
        setupUI()
        
        // 加载网络数据
        loadData()
    }
}

// MARK:- 加载网络数据
extension ZWLRecommandViewController {
    func loadData() {
        // 请求推荐数据
        RemVM.requestData { 
            self.collectionView.reloadData()
            
            self.gameView.groups = self.RemVM.anchorGroups
            
            self.loadDataFinished()
        }
        
        // 请求轮播图片数据
        RemVM.requestCycleData {
             self.cycleView.cycleModels = self.RemVM.cycleModels
        }
    }
}

// MARK:- collectionView的数据源和代理方法
extension ZWLRecommandViewController : UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return RemVM.anchorGroups.count
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let group = RemVM.anchorGroups[section]
        
        return group.anchors.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        // 取出模型对象
        let group = RemVM.anchorGroups[indexPath.section]
        let anchor = group.anchors[indexPath.item]

        if indexPath.section == 1 {
            var cell = ZWLContentHighCell()
            cell = collectionView.dequeueReusableCell(withReuseIdentifier: KRemHighhCellID, for: indexPath) as! ZWLContentHighCell
            cell.anchor = anchor
             return cell
        } else {
            var cell = ZWLContentCell()
            cell = collectionView.dequeueReusableCell(withReuseIdentifier: KRemCellID, for: indexPath) as! ZWLContentCell
            cell.anchor = anchor
            return cell
        }
    
    }
    
    // 返回一个头部视图
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
      
        let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: kHeaderViewID, for: indexPath as IndexPath) as! ZWLHeaderCollectionViewCell
        
        headerView.group = RemVM.anchorGroups[indexPath.section]
        
        return headerView
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if indexPath.section == 1 {
            return CGSize(width: KItemW, height: kHighItemH)
        }
        
        return CGSize(width: KItemW, height: KItemH)
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let a =  RemVM.anchorGroups[indexPath.section]
        let b = a.anchors[indexPath.item]
        b.isVertical == 0 ? setupPC() : setupPhone()
        
    }
    
    fileprivate func setupPhone() {
        
        let vc = ZWLPhoneViewController()
        
        present(vc, animated: true, completion: nil)
        
    }
    
    fileprivate func setupPC() {
        let vc = ZWLPCViewController()
        navigationController?.pushViewController(vc, animated: true)  
    }
}

// MARK:- UI 布局
extension ZWLRecommandViewController {
     internal override func setupUI() {
        
        contentView = collectionView
    
        view.addSubview(collectionView)
        collectionView.addSubview(cycleView)
        collectionView.addSubview(gameView)
        view.addSubview(liveBtn)
        
        super.setupUI()
    }
}
